package cn.iocoder.an.module.mold.controller.admin.rechecktobe;

import cn.iocoder.an.framework.common.exception.ServiceException;
import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.rechecktobe.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.rechecktobe.RecheckTobeDO;
import cn.iocoder.an.module.mold.service.rechecktobe.RecheckTobeService;

@Tag(name = "管理后台 - 待复检")
@RestController
@RequestMapping("/mold/recheck-tobe")
@Validated
public class RecheckTobeController {

    @Resource
    private RecheckTobeService recheckTobeService;

    @PostMapping("/create")
    @Operation(summary = "创建待复检")
    @PreAuthorize("@ss.hasPermission('mold:recheck-tobe:create')")
    public CommonResult<Long> createRecheckTobe(@Valid @RequestBody RecheckTobeSaveReqVO createReqVO) {
        return success(recheckTobeService.createRecheckTobe(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新待复检")
    @PreAuthorize("@ss.hasPermission('mold:recheck-tobe:update')")
    public CommonResult<Boolean> updateRecheckTobe(@Valid @RequestBody RecheckTobeSaveReqVO updateReqVO) {
        recheckTobeService.updateRecheckTobe(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除待复检")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:recheck-tobe:delete')")
    public CommonResult<Boolean> deleteRecheckTobe(@RequestParam("id") Long id) {
        recheckTobeService.deleteRecheckTobe(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除待复检")
                @PreAuthorize("@ss.hasPermission('mold:recheck-tobe:delete')")
    public CommonResult<Boolean> deleteRecheckTobeList(@RequestParam("ids") List<Long> ids) {
        recheckTobeService.deleteRecheckTobeListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得待复检")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:recheck-tobe:query')")
    public CommonResult<RecheckTobeRespVO> getRecheckTobe(@RequestParam("id") Long id) {
        RecheckTobeDO recheckTobe = recheckTobeService.getRecheckTobe(id);
        return success(BeanUtils.toBean(recheckTobe, RecheckTobeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得待复检分页")
    @PreAuthorize("@ss.hasPermission('mold:recheck-tobe:query')")
    @TransMethodResult
    public CommonResult<PageResult<RecheckTobeRespVO>> getRecheckTobePage(@Valid RecheckTobePageReqVO pageReqVO) {
        PageResult<RecheckTobeDO> pageResult = recheckTobeService.getRecheckTobePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RecheckTobeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出待复检 Excel")
    @PreAuthorize("@ss.hasPermission('mold:recheck-tobe:export')")
    @ApiAccessLog(operateType = EXPORT)
    @TransMethodResult
    public void exportRecheckTobeExcel(@Valid RecheckTobePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecheckTobeDO> list = recheckTobeService.getRecheckTobePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "待复检.xls", "数据", RecheckTobeRespVO.class,
                        BeanUtils.toBean(list, RecheckTobeRespVO.class));
    }



    @PutMapping("/dispatch")
    @Operation(summary = "派工接口")
    @TransMethodResult
    public CommonResult<Boolean> dispatch(@Valid @RequestBody RecheckTobeDispatchVO dispatchVO) {
        return success(recheckTobeService.dispatch(dispatchVO));
    }

    @PutMapping("/cancel-dispatch")
    @Operation(summary = "撤销派工接口")
    @TransMethodResult
    public CommonResult<Boolean> cancelDispatch(@RequestParam("id") Long id) {
        try {
            return success(recheckTobeService.cancelDispatch(id));
        } catch (ServiceException e) {
            return CommonResult.error(e.getCode(), e.getMessage());
        }
    }

    @PutMapping("/audit")
    @Operation(summary = "审核接口")
    @TransMethodResult
    public CommonResult<Boolean> audit(@Valid @RequestBody RecheckTobeAuditVO auditVO) {
        try {
            return success(recheckTobeService.audit(auditVO));
        } catch (ServiceException e) {
            return CommonResult.error(e.getCode(), e.getMessage());
        }
    }

}